//
//  TVCommodityCouponCell.swift
//  StarTimesON
//
//  Created by Toj on 12/1/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit
 
protocol TVCommodityCouponCellDelegate: NSObjectProtocol {
    func commodityCouponCellDidTapCoupons(_ view: TVCommodityCouponCell)
}

class TVCommodityCouponCell: UICollectionViewCell {
    
    weak var delegate: TVCommodityCouponCellDelegate?
    
    var cellModel: TVCouponViewModel? { didSet {
        guard let model = cellModel else { return }
        
        tipsLable.text = model.tips
    } }
    
    @objc private func didTapCoupons() {
        delegate?.commodityCouponCellDidTapCoupons(self)
    }
    
    private lazy var couponImageView: UIImageView = {
       let view = UIImageView()
        view.image = "commodity_tv_coupon_icon".to_image
        return view
    }()

    private lazy var couponLabel: UILabel = {
       let view = UILabel()
        view.text = "coupon".localized().uppercased() + ":"
        view.font = 12.ui_boldFont
        view.textColor = "#666666".to_color
        return view
    }()

    private lazy var arrowsImageView: UIImageView = {
       let view = UIImageView()
        view.image = "commodity_arrows".to_image
        return view
    }()
    
    private lazy var tipsLable: UILabel = {
       let view = UILabel()
        view.numberOfLines = 2
        view.textAlignment = .right
        view.textColor = "#775E33".to_color
        view.font = 12.to_systemFont
        return view
    }()
    
    private lazy var couponsButton: UIButton = {
        let view = UIButton()
        view.addTarget(self, action: #selector(didTapCoupons), for: .touchUpInside)
        return view
    }()
 
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupMakeAddSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()

        setupMakeLayoutSubviews()
    }
}

// MARK: - 初始化
extension TVCommodityCouponCell {
    
    private func setupMakeAddSubviews() {
          
        contentView.addSubview(tipsLable)
        contentView.addSubview(arrowsImageView)
        contentView.addSubview(couponLabel)
        contentView.addSubview(couponImageView)
        contentView.addSubview(couponsButton)
    }
    
    private func setupMakeLayoutSubviews() {
        
        couponImageView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.centerY.equalTo(contentView)
            make.height.equalTo(24)
            make.width.equalTo(24)
        }
        couponLabel.sizeToFit()
        let width = couponLabel.frame.width
        couponLabel.snp.remakeConstraints { make in
            make.left.equalTo(couponImageView.snp.right).offset(6)
            make.width.equalTo(width)
            make.centerY.equalTo(couponImageView)
        }
        
        arrowsImageView.snp.makeConstraints { make in
            make.right.equalTo(snp.right).offset(-16)
            make.centerY.equalTo(contentView)
            make.height.equalTo(24)
            make.width.equalTo(24)
        }
        
        tipsLable.snp.makeConstraints { make in
            make.left.equalTo(couponLabel.snp.right).offset(2)
            make.right.equalTo(arrowsImageView.snp.left).offset(-2)
            make.centerY.equalTo(contentView)
        }
        
        couponsButton.snp.makeConstraints { make in
            make.left.equalTo(tipsLable)
            make.top.bottom.right.equalTo(contentView)
        }
    }
}
